package com.aispeech.asr.apis.ssc.callback;

import com.aispeech.asr.apis.ssc.websocket.connection.EmitterManager;
import com.aispeech.jarpc.ArpcException;
import com.aispeech.jarpc.IArpcCallback;
import com.aispeech.jarpc.TextBin;
import com.aispeech.jarpc.message.ArpcMessage;
import com.aispeech.jarpc.op.sd.SdResult;
import com.alibaba.fastjson.JSON;

import lombok.extern.slf4j.Slf4j;


@Slf4j
public class SdOnlineCallback implements IArpcCallback {

    private EmitterManager manager;
    private String clientSessionId;
	
    public SdOnlineCallback(String clientSessionId, EmitterManager manager) {
    	this.manager = manager;
    	this.clientSessionId = clientSessionId;
    }
	
	@Override
	public void onArpcMessage(ArpcMessage message) {
		try {
			
			//String arpcMessage = new String(message.getBody(),"utf-8");
			//log.info("[SdOnlineCallback] =======arpcMessage:{} ",arpcMessage);
			
			SdResult sdResult = JSON.parseObject(message.getBody(), SdResult.class);
			manager.doResponse(clientSessionId, sdResult, 0);
		}catch (Exception e) {
			log.error("[SedOnlineCallback] failed sessionId:{}",clientSessionId,e);
		}
	}
	
	@Override
	public void onArpcError(ArpcException exception) {
		log.error("[SdOnlineCallback onArpcError]",exception);
		manager.doErrorResponse(clientSessionId, exception);
		
	}

}
